package com.swpu.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swpu.project.common.BaseResponse;
import com.swpu.project.common.DeleteRequest;
import com.swpu.project.common.ErrorCode;
import com.swpu.project.common.ResultUtils;
import com.swpu.project.exception.BusinessException;
import com.swpu.project.model.dto.ebrissecondary.EbrisSecondaryAddRequest;
import com.swpu.project.model.dto.ebrissecondary.EbrisSecondaryQueryRequest;
import com.swpu.project.model.dto.ebrissecondary.EbrisSecondaryUpdateRequest;
import com.swpu.project.model.entity.EbrisSecondary;
import com.swpu.project.service.EbrisSecondaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 原生岩屑接口
 *
 */
@RestController
@RequestMapping("/ebrisSecondary")
public class EbrisSecondaryController {

    @Resource
    private EbrisSecondaryService ebrisSecondaryService;



    /**
     * 创建用户
     *
     * @param ebrisSecondaryAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<String> addEbrisSecondary(@RequestBody EbrisSecondaryAddRequest ebrisSecondaryAddRequest, HttpServletRequest request) {
        if (ebrisSecondaryAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        EbrisSecondary ebrisSecondary = new EbrisSecondary();
        BeanUtils.copyProperties(ebrisSecondaryAddRequest, ebrisSecondary);
        boolean result = ebrisSecondaryService.save(ebrisSecondary);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(ebrisSecondary.getSecondaryId());
    }

    /**
     * 删除用户
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteEbrisSecondary(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = ebrisSecondaryService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新用户
     *
     * @param ebrisSecondaryUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateEbrisSecondary(@RequestBody EbrisSecondaryUpdateRequest ebrisSecondaryUpdateRequest, HttpServletRequest request) {
        if (ebrisSecondaryUpdateRequest == null || ebrisSecondaryUpdateRequest.getNativeId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        EbrisSecondary ebrisSecondary = new EbrisSecondary();
        BeanUtils.copyProperties(ebrisSecondaryUpdateRequest, ebrisSecondary);
        boolean result = ebrisSecondaryService.updateById(ebrisSecondary);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取用户
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<EbrisSecondary> getEbrisSecondaryById(int id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        EbrisSecondary ebrisSecondary = ebrisSecondaryService.getById(id);
        return ResultUtils.success(ebrisSecondary);
    }

    /**
     * 获取用户列表
     *
     * @param ebrisSecondaryQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<EbrisSecondary>> listEbrisSecondary(EbrisSecondaryQueryRequest ebrisSecondaryQueryRequest, HttpServletRequest request) {
        EbrisSecondary ebrisSecondaryQuery = new EbrisSecondary();
        if (ebrisSecondaryQueryRequest != null) {
            BeanUtils.copyProperties(ebrisSecondaryQueryRequest, ebrisSecondaryQuery);
        }
        QueryWrapper<EbrisSecondary> queryWrapper = new QueryWrapper<>(ebrisSecondaryQuery);
        List<EbrisSecondary> ebrisSecondaryList = ebrisSecondaryService.list(queryWrapper);
        return ResultUtils.success(ebrisSecondaryList);
    }

    /**
     * 分页获取用户列表
     *
     * @param ebrisSecondaryQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<EbrisSecondary>> listEbrisSecondaryByPage(EbrisSecondaryQueryRequest ebrisSecondaryQueryRequest, HttpServletRequest request) {
        long current = 1;
        long size = 10;
        EbrisSecondary ebrisSecondaryQuery = new EbrisSecondary();
        if (ebrisSecondaryQueryRequest != null) {
            BeanUtils.copyProperties(ebrisSecondaryQueryRequest, ebrisSecondaryQuery);
            current = ebrisSecondaryQueryRequest.getCurrent();
            size = ebrisSecondaryQueryRequest.getPageSize();
        }
        QueryWrapper<EbrisSecondary> queryWrapper = new QueryWrapper<>(ebrisSecondaryQuery);
        Page<EbrisSecondary> ebrisSecondaryPage = ebrisSecondaryService.page(new Page<>(current, size), queryWrapper);
        return ResultUtils.success(ebrisSecondaryPage);
    }

}
